package com.friend.chat.page.vm

import androidx.lifecycle.MutableLiveData
import com.friend.chat.base.BasicViewModel
import com.friend.chat.net.BaseResult
import com.friend.chat.page.bean.FriendBean
import com.google.gson.Gson
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody

/**
 * @des
 * @date 2021/10/8
 * @author gorilla
 */
class AddGroupViewModel : BasicViewModel() {
    //通讯录
    val contactListResponse by lazy { MutableLiveData<BaseResult<List<FriendBean>>>() }

    fun getContactList(userId: String) {
        netWorkJob(
            successResult = {
                val data = mRepository.getFriendsList(userId)
                if (!data.isOk()) {
                    toast(data.message)
                } else {
                    contactListResponse.value = data
                }
            },
            errorResult = {
                toast("错误")
            }
        )
    }

    //添加好友
    val addFriendResponse by lazy { MutableLiveData<BaseResult<Boolean>>() }

    fun createGroup(uid: String,groupName:String, memberIdList: ArrayList<String>) {
        val map = HashMap<String,Any>(5)
        map["uid"] = uid
        map["groupName"] =groupName
        map["memberIdList"] = memberIdList
        val toJson = Gson().toJson(map)
        val body: RequestBody =
            toJson.toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
        netWorkJob(
            successResult = {
                val data = mRepository.createGroup( body)
                if (!data.isOk()) {
                    toast(data.message)
                } else {
                    addFriendResponse.value = data
                }
            },
            errorResult = {
                toast("错误")
            }
        )
    }
}